<!-- 功能：添加接收邮箱地址 弹窗 | 作者：menghao.yan | 邮箱：yxs2615@163.com | 时间：2023年08月21日 22:59:06 -->
<template>
  <!-- 弹出框 -->
  <a-modal v-model:open="visible" title="添加接收邮箱地址" width="500px" class="y-modal" okText="创建" @ok="onSubmit" @cancel="onCancel">
    <a-form :label-col="{ span: 6 }" :wrapper-col="{span: 14}">
      <a-form-item label="用户名" v-bind="validateInfos.userName">
        <a-input v-model:value="modelRef.userName" placeholder="请输入用户名" />
      </a-form-item>
      <a-form-item label="接收邮箱地址" v-bind="validateInfos.emailAddress">
        <a-input v-model:value="modelRef.emailAddress" placeholder="请输入接收邮箱地址" />
      </a-form-item>
      <a-form-item label="启用">
        <a-checkbox v-model:checked="modelRef.qiYong"></a-checkbox>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const { visible } = toRefs(props)

// 定义组件的事件
const emits = defineEmits(['update:visible'])

import { Form } from 'ant-design-vue'

const useForm = Form.useForm

const modelRef = reactive({
  userName: '',
  emailAddress: '',
  qiYong: false
})

const rulesRef = reactive({
  userName: [
    {
      required: true,
      message: '请输入用户名'
    }
  ],
  emailAddress: [
    {
      required: true,
      message: '请输入接收邮箱地址'
    }
  ]
})

const { resetFields, validate, validateInfos, mergeValidateInfo } = useForm(modelRef, rulesRef)
const onSubmit = () => {
  validate()
    .then(() => {
      console.log('添加接收邮箱地址>>', toRaw(modelRef))
      onCancel()
    })
    .catch(err => {
      console.log('error', err)
    })
}

const onCancel = () => {
  // 触发自定义事件来通知父组件关闭弹窗
  emits('update:visible', false)
}

</script>

<style scoped>
.ant-row {
  margin-bottom: 10px;
}
.y-xinJianYongHu-title {
  color: #333;
  font-weight: bold;
  font-size: 16px;
}
</style>